/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 6, 2011    @time-created 9:55:00 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package main;

import util.enumeration.Country;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.naming.InitialContext;
import session.stateless.TouristAccountSessionBean;
import util.enumeration.Gender;


public class TestTourism {
    BufferedReader keyboardReader = null;
    
    TouristAccountSessionBean touristAccountMgr;
//    CourseDetailsManagerRemote courseMgr;
//    RegistrationDetailsManagerRemote registrationMgr;


    /** Creates a new instance of Main */
    public TestTourism() {
        keyboardReader = new BufferedReader (new InputStreamReader(System.in));
    }

    public static void main(String[] args) {
        TestTourism client = new TestTourism();
        client.getSessionBean();
        client.displayMenu();
    }

    private void getSessionBean() {
        try {
            InitialContext ic = new InitialContext();
            touristAccountMgr = (TouristAccountSessionBean)
                    ic.lookup(TouristAccountSessionBean.class.getName());
//            tutorMgr = (TouristManagerRemote)
//                    ic.lookup(TutorDetailsManagerRemote.class.getName());
//            courseMgr = (CourseDetailsManagerRemote)
//                    ic.lookup(CourseDetailsManagerRemote.class.getName());
//            registrationMgr = (RegistrationDetailsManagerRemote)
//                    ic.lookup(RegistrationDetailsManagerRemote.class.getName());
        } catch (Exception ex) {
            System.err.println("Initial Context lookup exception!");
            ex.printStackTrace();
        }
    }

    private void displayMenu() {
        String userInput = "";
        char quitMain = ' ';

        try {
            do {
                userInput = "";
                quitMain = ' ';

                System.out.println("==========================================");
                System.out.println("   WELCOME TO TOURISM CMS TESTING CENTRE     ");
                System.out.println("==========================================\n");
                System.out.println("Please select:\n");
                System.out.println("a.\tInsert Tourist");
                System.out.println("b.\tDelete a Tourist");
//                System.out.println("c.\tAdd a Tutor");
//                System.out.println("d.\tDelete a Tutor");
//                System.out.println("e.\tAdd a Course");
//                System.out.println("f.\tDelete a Course");
                System.out.println("q.\tExit Application\n");
                
                do {
                    System.out.print("Choice: ");
                    // get choice from console
                    userInput = keyboardReader.readLine().toLowerCase().trim();

                } while (!(userInput.equalsIgnoreCase("a") || userInput.equalsIgnoreCase("b") ||
//                         userInput.equalsIgnoreCase("c") || userInput.equalsIgnoreCase("d") ||
//                         userInput.equalsIgnoreCase("e") || userInput.equalsIgnoreCase("f") ||
                         userInput.equalsIgnoreCase("q")));

                if (userInput.equalsIgnoreCase("q")) {
                        quitMain = 'q';
                }
                else
                {
                    char choice = userInput.charAt(0);
                    switch (choice) {
                        case 'a':  addTourist();       break;
                        case 'b':  deleteTourist();    break;
//                        case 'c':  addTutor();         break;
//                        case 'd':  deleteTutor();      break;
//                        case 'e':  addCourse();        break;
//                        case 'f':  deleteCourse();     break;
                    }

                }

                System.out.println("\n");
                
            } while (quitMain != 'q');

            if (quitMain == 'q') {
                touristAccountMgr.remove();
//                tutorMgr.remove();
//                courseMgr.remove();
                System.out.println("\nTerminated. Exiting...\n");
            }
        }
        catch (IOException ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
        catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }

    public void addTourist() {
        String userInput = "";
        String username = "";
        String password = "";
        String lastName = "";
        String firstName = "";
        Gender gender = null;
        Date birthdate = null;
        Country country = null;
        String locale = "";
        String email = "";
        String emailSecondary = "";

        try {
            do {
                System.out.print("\nEnter Desired Username (email): ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toLowerCase();
                username = userInput;
                email = userInput;
            } while (userInput.isEmpty());

            do {
                System.out.print("Enter Password: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim();
                password = userInput;
            } while (userInput.isEmpty());
            
            do {
                System.out.print("\nEnter Last Name: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                lastName = userInput;
            } while (userInput.isEmpty());

            do {
                System.out.print("Enter First Name: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                firstName = userInput;
            } while (userInput.isEmpty());

            do {
                System.out.print("Enter Gender (M/F): ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                if (!Gender.valueOf(userInput).equals((Object)userInput)) {
                    gender = Gender.valueOf(userInput);
                }
//                gender = userInput.charAt(0);
            } while (userInput.isEmpty());
            
            do {
                System.out.print("Enter Birthdate: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                birthdate = sdf.parse(userInput);
            } while (userInput.isEmpty());
            
            do {
                System.out.print("Enter Country: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                if (!Country.valueOf(userInput).equals((Object)userInput)) {
                    country = Country.valueOf(userInput);
                }
            } while (userInput.isEmpty());
            
            do {
                System.out.print("Enter Locale (e.g. en_US): ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                locale = userInput;
            } while (userInput.isEmpty());
            
            do {
                System.out.print("Enter Secondary Email: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                emailSecondary = userInput;
            } while (userInput.isEmpty());

     //       touristAccountMgr.createTouristAccount(username, password, lastName, firstName, gender, birthdate, country, locale, email, emailSecondary);
            
//            studentMgr.createAddress(addr1, addr2, postCode);
//            studentMgr.createContactNumber(phone);
            
            String acctId = touristAccountMgr.getTouristAccountByUsername(username).getId().toString();
            if (acctId.isEmpty()) {
                // print exception
            }
            else {
                System.out.println("\n(" + acctId + ") " + username + " " + password
                        + " Tourist " + lastName + " " + firstName + " ("
                        + gender + ") " + "(" + birthdate + ") successfully created!\n");
            }

        }
        catch (IOException ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
//        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
//        }
        catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }
    
    public void deleteTourist() {
        String userInput = "";
        String username = "";

        try {
//            printStudents(touristMgr);

            do {
                System.out.print("\nEnter Account Username: ");
                userInput = "";
                userInput = keyboardReader.readLine().trim().toUpperCase();
                username = userInput;
            } while (userInput.isEmpty());

            String acctId = touristAccountMgr.getTouristAccountByUsername(username).getId().toString();
            if (acctId.isEmpty()) {
                // print exception
            }
            else {
//                touristAccountMgr.deleteTouristAccount(Long.getLong(acctId));
            }

            System.out.println("\nTourist Account with username " + username + " successfully deleted!\n");
        }
        catch (IOException ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

//    public void addTutor() {
//        String userInput = "";
//        String id = "";
//        String name = "";
//
//        try {
//            do {
//                System.out.print("\nEnter Tutor ID: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                id = userInput;
//            } while (userInput.isEmpty());
//
//            do {
//                System.out.print("Enter Tutor Name: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                name = userInput;
//            } while (userInput.isEmpty());
//
//            tutorMgr.createTutor(id, name);
//            tutorMgr.persist();
//
//            System.out.println("\nTutor ID " + id + " (" + name + ") successfully created!\n");
//        }
//        catch (IOException ex) {
//            System.out.println("ERROR: " + ex.getMessage());
//        }
//        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
//        }
//        catch (Exception ex) {
//            System.out.println("ERROR: " + ex.getMessage());
//        }
//    }

//    public void deleteTutor() {
//        String userInput = "";
//        String id = "";
//
//        try {
//            printTutors(tutorMgr.getTutors());
//            
//            do {
//                System.out.print("\nEnter Tutor ID: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                id = userInput;
//            } while (userInput.isEmpty());
//
//            tutorMgr.deleteTutor(id);
//
//            System.out.println("\nTutor ID " + id + " successfully deleted!\n");
//        }
//        catch (IOException ex) {
//            System.out.println("ERROR [IO]: " + ex.getMessage());
//        }
//        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
//        }
//        catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
//    }

//    public void addCourse() {
//        String userInput = "";
//        String name = "";
//        Date startDate;
//        Date endDate;
//        int seats = 0;
//        String tutorId = "";
//        HashSet<String> tutorIds = new HashSet<String>();
//
//        try {
//            // check if any tutors first before allowing to enter values,
//            // will throw exception automatically when no tutors found
//            if (tutorMgr.getTutors().isEmpty()) {
//                //
//            }
//            else {
//                do {
//                    System.out.print("\nEnter Course Name: ");
//                    userInput = "";
//                    userInput = keyboardReader.readLine().trim().toUpperCase();
//                    name = userInput;
//                } while (userInput.isEmpty());
//
//                do {
//                    System.out.print("Enter Start Date (DD-MM-YYYY): ");
//                    userInput = "";
//                    userInput = keyboardReader.readLine().trim();
//    //                Calendar cal = new GregorianCalendar(2011, Calendar.APRIL, 1);
//    //                startDate = cal.getTime();
//                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
//                    startDate = sdf.parse(userInput);
//                } while (userInput.isEmpty());
//
//                do {
//                    System.out.print("Enter End Date (DD-MM-YYYY): ");
//                    userInput = "";
//                    userInput = keyboardReader.readLine().trim();
//                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
//                    endDate = sdf.parse(userInput);
//                } while (userInput.isEmpty());
//
//                do {
//                    System.out.print("Enter Class Size: ");
//                    userInput = "";
//                    userInput = keyboardReader.readLine().trim();
//                    seats = Integer.parseInt(userInput);
//                } while (userInput.isEmpty());
//
//                System.out.print("\n----- Associate Tutor(s) with Course -----\n");
//                boolean more = true;
//                boolean yesNo = true;
//                do {
//                    try {
//                        printTutors(tutorMgr.getTutors());
//
//                        do {
//                            System.out.print("\nEnter Tutor ID: ");
//                            userInput = "";
//                            userInput = keyboardReader.readLine().trim().toUpperCase();
//                            tutorId = userInput;
//                            tutorIds.add(tutorId);
//                        } while (userInput.isEmpty());
//
//                        do {
//                            do {
//                                System.out.print("\nDo you want to add more tutors (Y/N)? ");
//                                userInput = "";
//                                userInput = keyboardReader.readLine().toLowerCase().trim();
//
//                                if (userInput.equalsIgnoreCase("y")) {
//                                    more = true;
//                                    yesNo = true;
//                                }
//                                else
//                                if (userInput.equalsIgnoreCase("n")) {
//                                    more = false;
//                                    yesNo = true;
//                                }
//                                else {
//                                    yesNo = false;
//                                }
//                            } while (userInput.isEmpty());
//                        } while (yesNo == false);
//
//                    } catch (IOException ex) {
//                        System.out.println("\nERROR [IO]: " + ex.getMessage());
//                    } catch (ExistException ex) {
//                        System.out.println(ex.getMessage());
//                    } catch (Exception ex) {
//                        System.out.println("\nERROR: " + ex.getMessage());
//                    }
//                } while (more);
//
//                courseMgr.createCourse(name, startDate, endDate);
//                courseMgr.createClass(seats);
//                courseMgr.associateTutors(tutorIds);
//                courseMgr.persist();
//
//                String cId = courseMgr.getCourseByName(name).getCourseId().toString();
//
//                System.out.println("\nCourse ID " + cId + " (" + name + ") with " + seats + " seats successfully created!\n");
//            }
//        }
//        catch (IOException ex) {
//            System.out.println("ERROR [IO]: " + ex.getMessage());
//        }
//        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
//        }
//        catch (ParseException ex) {
//            System.out.println("ERROR: " + ex.getMessage());
//        }
//    }
//
//    public void deleteCourse() {
//        String userInput = "";
//        Long id;
//
//        try {
//            do {
//                printCourses(courseMgr.getCourses());
//                
//                System.out.print("\nEnter Course ID: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                id = Long.parseLong(userInput);
//            } while (userInput.isEmpty());
//
//            courseMgr.deleteCourse(id);
//
//            System.out.println("\nCourse " + id + " successfully deleted!\n");
//        }
//        catch (IOException ex) {
//            System.out.println("ERROR [IO]: " + ex.getMessage());
//        }
//        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
//        }
//    }
//
//    // display list of all students
//    public void printStudents(Set<StudentDetailsState> students) {
//        String displaySet = "";
//
//        displaySet = "\nStudent ID\tStudent Name\n"
//                + "------------------------------------------\n";
//        for (StudentDetailsState sd : students) {
//            displaySet += sd.getStudentId();
//            displaySet += "\t";
//            displaySet += sd.getName();
//            displaySet += "\n";
//        }
//
//        System.out.println(displaySet);
//    }
//    
//    // display list of all tutors
//    public void printTutors(Set<TutorDetailsState> tutors) {
//        String displaySet = "";
//
//        displaySet = "Tutor ID\tTutor Name\n"
//                + "------------------------------------------\n";
//        for (TutorDetailsState td : tutors) {
//            displaySet += td.getTutorId();
//            displaySet += "\t";
//            displaySet += td.getTutorName();
//            displaySet += "\n";
//        }
//
//        System.out.println(displaySet);
//    }
//
//    // display list of all courses
//    public void printCourses(Set<CourseDetailsState> courses) {
//        String displaySet = "";
//
//        displaySet = "\nCourse ID\tCourse Name\n"
//                + "------------------------------------------\n";
//        for (CourseDetailsState cd : courses) {
//            displaySet += cd.getCourseId();
//            displaySet += "\t\t";
//            displaySet += cd.getCourseName();
//            displaySet += "\n";
//        }
//
//        System.out.println(displaySet);
//    }
}
